package com.view;

import com.model.Anggota;
import com.model.Perpustakaan;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class PanelTambahAnggota extends JPanel {

    private JLabel labelPanel;
    private JLabel labelKodeAnggota;
    private JTextField textKodeAnggota;
    private JLabel labelNama;
    private JTextField textNama;
    private JButton tombolSimpan;
    private JButton tombolBatal;

    public PanelTambahAnggota() {
        setLayout(null);

        labelPanel = new JLabel("Form Tambah Anggota");
        labelPanel.setBounds(100, 5, 220, 50);
        add(labelPanel);

        labelKodeAnggota = new JLabel("Kode Anggota");
        labelKodeAnggota.setBounds(10, 50, 100, 50);
        add(labelKodeAnggota);

        textKodeAnggota = new JTextField();
        textKodeAnggota.setBounds(100, 65, 100, 25);
        add(textKodeAnggota);

        labelNama = new JLabel("Nama");
        labelNama.setBounds(10, 85, 50, 50);
        add(labelNama);

        textNama = new JTextField();
        textNama.setBounds(100, 100, 200, 25);
        add(textNama);

        tombolSimpan = new JButton("Simpan");
        tombolSimpan.setBounds(50, 155, 100, 30);
        add(tombolSimpan);
        tombolSimpan.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                tombolSimpanAction();
            }
        });

        tombolBatal = new JButton("Batal");
        tombolBatal.setBounds(170, 155, 100, 30);
        add(tombolBatal);
        tombolBatal.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                tombolBatalAction();
            }
        });
    }

    public void tombolSimpanAction(){
       Anggota ang = new Anggota();
       try{
       ang.setIdAnggota(textKodeAnggota.getText());
       }catch (Exception ex){
           JOptionPane.showMessageDialog(null, ex.getMessage());
           Logger.getLogger(PanelTambahPinjaman.class.getName()).log(Level.SEVERE, null, ex);
       }
       ang.setNama(textNama.getText());
       Perpustakaan.tambahAnggota(ang);
       JOptionPane.showMessageDialog(this, "DATA ANGGOTA" + "\n\nId Anggota: " + ang.getIdAnggota()
                                     + "\nNama: " + ang.getNama());
    }

    public void tombolBatalAction(){
        System.exit(0);
    }
    
}
